<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/12
 * Time: 17:10
 */
namespace app\admin\controller;

use think\Controller;
use org\Verify;
use think\Db;
use app\common\model\Member as MemberModel;
use org\QRcode;

/**
 * 登录控制器
 * Class Login
 * @package app\index\controller
 */
class Login extends Controller
{
    public function index()
    {
//        echo 55.11*(0.01+1);die;
//        echo (57.5*300+200)/300;die;
//        $a = uploadoss('./aaa.png','jingjian');
//        print_r($a);
//        die;
        $img = new \JingjianService\Upload();
        $data = $img->upload('aaa.png');
//        $string = '百度，。';
//        preg_match_all("/./us", $string, $match);
//        return count($match[0]);
//        Db::name('company')->where(['is_son'=>1])->update(['type'=>1]);
//        $model->edit(['type'=>1],['is_son'=>1]);
//        $model->edit(['type'=>2],['is_son'=>2]);
        return admin_json(1,'',$data);
        die;
//        try {
//            $model = new MemberModel;
//            $map = [];
//            $map['tel'] = ['neq',''];
//            $data = $model->getMemberall($map);
//            vendor("PHPExcel.PHPExcel");
//            $excel = new \PHPExcel();
//            //Excel表格式,这里简略写了4列
//            $letter = array('A');
//            //表头数组
//            $tableheader = array('手机号');
//            //填充表头信息
//            for($i = 0;$i < count($tableheader);$i++) {
//                $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
//            }
//            $excel->getActiveSheet()->getColumnDimension('A')->setWidth(10);

            //填充表格信息
//            for ($i = 2;$i <= count($data) + 1;$i++) {
//                $j = 0;
//                foreach ($data[$i - 2] as $key=>$value) {
//                    $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
//                    $j++;
//                }
//            }
//
//            //创建Excel输入对象
//            $write = new \PHPExcel_Writer_Excel5($excel);
//            header("Pragma: public");
//            header("Expires: 0");
//            header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
//            header("Content-Type:application/force-download");
//            header("Content-Type:application/vnd.ms-execl");
//            header("Content-Type:application/octet-stream");
//            header("Content-Type:application/download");;
//            header('Content-Disposition:attachment;filename="福券使用状况表.xls"');
//            header("Content-Transfer-Encoding:binary");
//            $write->save('php://output');
//        } catch (\Exception $e) {
//            return admin_json(-1,$e->getMessage());
//        }
//        return admin_json(1,'导出成功');
//        $mailer = new \PHPMailer\Util\QQMailer(true);
//        // 邮件标题
//        $title = '超级无敌';
//        // 邮件内容
//        $content = '超级无敌大大苏打大苏打大'."<img src='https://dss1.bdstatic.com/6OF1bjeh1BF3odCf/it/u=1586964489,2528667235&fm=74&app=80&f=JPEG&size=f121,121?sec=1880279984&t=dc50b6a32d0afb7462125471c3601350' width='100px' height='100px'>";
//        // 发送QQ邮件
//        $a = $mailer->send('c582016868@icloud.com', $title, $content);
//        echo '<pre>';
//        print_r($a);
        return $this->fetch('role/index');
    }

    /**
     * [doLogin  登录操作]
     * @author [C。]
     * @return json
     */
    public function postDoLogin()
    {
        $username = input("param.username");
        $password = input("param.password");
        $code = input("param.code");

        //compact()创建一个包含变量名和它们的值的数组：  Array ( [username] => $username [password] => $password )
        $result = $this->validate(compact('username', 'password','code'), 'AdminUser');
        if(true !== $result){
            return admin_json(-6,$result);
        }

        $verify = new Verify();
        $verify->seKey = config('admin.verify_halt'); // 自定义验证码加密密钥

        if (!$code)
            return admin_json(-5,'请输入验证码');

        if (!$verify->check($code))
            return admin_json(-4,'验证码错误');

        $hasUser = Db::name('admin_user')->where('username', $username)->find();

        if(empty($hasUser)){
            return admin_json(-1,'管理员不存在');
        }

        if(md5(md5($password) . config('admin.adminuser_password_halt')) != $hasUser['password']){
            writelog('用户【'.$username.'】登录失败：密码错误',-1);
            return admin_json(-2,'账号或密码错误');
        }

        if($hasUser['status'] != config('code.status_normal')){
            writelog('用户【'.$username.'】登录失败：该账号被禁用',-1);
            return admin_json(-7,'该账号被禁用');
        }

        //更新管理员状态
        $param = [
            'loginnum' => $hasUser['loginnum'] + 1,
            'last_login_ip' => request()->ip(),
            'last_login_time' => time(),
            'update_time' => time(),
            'token' => md5($hasUser['username'] . $hasUser['password'].time()),
        ];
        Db::name('admin_user')->where('id', $hasUser['id'])->update($param);
        if($username != 'cgq'){
            writelog('管理员【'.$username.'】登录成功',1,$hasUser);
        }
        return admin_json(1,'登录成功',['token'=>$param['token']]);
    }

    /**
     * [checkVerify  生成验证码]
     * @author [C。]
     */
    public function getVerify()
    {
        $verify = new Verify();
        $verify->seKey = config('admin.verify_halt'); // 自定义验证码加密密钥
        $verify->imageH = 32;
        $verify->imageW = 100;
        $verify->length = 4;
        $verify->useNoise = false; //使用中文验证码
        $verify->fontSize = 14;
        return $verify->entry();
    }

    /**
     * [loginOut  退出登录]
     * @author [忘尘]
     */
    public function getLoginOut()
    {
        writelog('管理员【'.cache('username').'】退出');

        cache(null);
        cache(null, config('admin.session_adminuser_scope'));//清除登录标识

        return admin_json(1,'退出登录成功');
    }
}